package com.example.mydiyview.DiyView;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.example.mydiyview.R;

public class InputNumberView extends RelativeLayout {
    private int num = 0;
    private View viewAdd, viewDel;
    private EditText viewVal;
    private int max, defval, step, color;
    private  int mim ;
    private boolean disable;
    private float dimension;

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public int getDefval() {
        return defval;
    }

    public void setDefval(int defval) {
        this.defval = defval;
    }

    public int getStep() {
        return step;
    }

    public void setStep(int step) {
        this.step = step;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public int getMim() {
        return mim;
    }

    public void setMim(int mim) {
        this.mim = mim;
    }

    public boolean isDisable() {
        return disable;
    }

    public void setDisable(boolean disable) {
        this.disable = disable;
    }

    public float getDimension() {
        return dimension;
    }

    public void setDimension(float dimension) {
        this.dimension = dimension;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public InputNumberView(Context context) {
        this(context, null);
    }

    public InputNumberView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InputNumberView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        获取相关属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.InputNumberView);
        max = typedArray.getInt(R.styleable.InputNumberView_max,0);
        mim = typedArray.getInt(R.styleable.InputNumberView_min,0);
        defval = typedArray.getInt(R.styleable.InputNumberView_defval,0);
        step = typedArray.getInt(R.styleable.InputNumberView_step,0);
        disable = typedArray.getBoolean(R.styleable.InputNumberView_disable, false);
        color = typedArray.getColor(R.styleable.InputNumberView_color, 0);
        dimension = typedArray.getDimension(R.styleable.InputNumberView_valuesize, 0F);
        Log.i("TAG", "InputNumberView: "+max);
        View view = LayoutInflater.from(context).inflate(R.layout.input_number_view, this, true);
        viewAdd = this.findViewById(R.id.add);
        viewDel = this.findViewById(R.id.del);
        viewVal = this.findViewById(R.id.val);

    }


    //    增加数的方法
    public void add(OnNumchanglistener onNumchanglistener) {
        viewAdd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (getNum()>=max){
                    Log.i("TAG", "onClick: max"+max);
                    return;
                }else {
                    num = num + 1;
                    setNum(num);
                    viewVal.setText(String.valueOf(getNum()));
                    onNumchanglistener.onNumval(getNum());
                }
            }
        });

    }

    //    减少数的方法
    public void del(OnNumchanglistener onNumchanglistener) {
        viewDel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (getNum()<=getMim()){
                    Log.i("TAG", "onClick: min"+getMim());
                    Log.i("TAG", "onClick: +");
                    return;

                }else {
                    Log.i("TAG", "onClick: _");
                    num = num - 1;
                    setNum(num);
                    viewVal.setText(String.valueOf(getNum()));
                    onNumchanglistener.onNumval(getNum());
                }
            }
        });
    }

    public interface OnNumchanglistener {
        void onNumval(int num);
    }

}
